package cloud.xlh.my_system.service.impl;

import cloud.xlh.my_system.common.enums.ResultEnum;
import cloud.xlh.my_system.common.response.Response;
import cloud.xlh.my_system.mapper.DatabaseMapper;
import cloud.xlh.my_system.mapper.GenTableColumnMapper;
import cloud.xlh.my_system.pojo.Database;
import cloud.xlh.my_system.pojo.GenTable;
import cloud.xlh.my_system.pojo.Project;
import cloud.xlh.my_system.service.DatabaseService;
import cloud.xlh.my_system.service.GenTableColumnService;
import cloud.xlh.my_system.service.GenTableService;
import cloud.xlh.my_system.service.ProjectService;
import cloud.xlh.my_system.utils.ConnectionTestHelper;
import cloud.xlh.my_system.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.*;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : code-generator
 * @ Author: XuLeHuang
 * @ Date: 2022/5/31 10:57
 * @ Description:
 */
@Service
@Transactional
public class DatabaseServiceImpl implements DatabaseService {
    @Autowired
    private DatabaseMapper dataBaseMapper;
    @Autowired
    private ConnectionTestHelper connectionTestHelper;
    @Autowired
    private GenTableService genTableService;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private GenTableColumnService genTableColumnService;

    @Override
    public Response<HashMap> getListData(Integer limit , Integer page) {
        List<Database> databaseList= dataBaseMapper.findAllDatabaseToPage(limit , page - 1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows" , databaseList);
        map.put("limit" , limit);
        map.put("total" , page);
        return Response.success(map);
    }

    @Override
    public Response<String> add(Database database) {
        //保存逻辑
        database.setId(idWorker.nextId() + "");
        database.setCreateDate(new Date());
        database.setUpdateDate(new Date());
        int rows = dataBaseMapper.add(database);
        if (rows <= 0) {
            return Response.error("保存失败！");
        }
        //导入与数据库相关的表
        String dbName = database.getDbName();
        genTableService.importTable(dbName , database.getId());
        return Response.success("保存成功！");
    }

    @Override
    public Response<Boolean> connectTest(Database database) {
        boolean connection = connectionTestHelper.connect(database, false);
        if(connection){
            return Response.success(ResultEnum.SUCCESS.getCode(), "连接成功", true);
        }else {
            return Response.fail("连接失败");
        }
    }

    @Override
    public Database findDatabaseById(Long databaseId) {
        boolean present = Optional.ofNullable(databaseId).isPresent();
        if (present) {
            Database  database = dataBaseMapper.findDatabaseById(databaseId);
            return database;
        }
        return null;
    }

    @Override
    public Response<List<Database>> option(Database database) {
        List<Database>  databaseList = dataBaseMapper.findAllDatabase();
        return Response.success(0 , "获取全部数据库成功" , databaseList);
    }

    @Override
    public Response<String> newTableLoading(Database database) {
        String databaseId = database.getId();
        //根据databaseId获取gen_table表中的表名：1:n
        List<GenTable> genTableList = genTableService.findAllTableByDatabaseId(databaseId);
        //根据gen_table表中的表名获取表id：多个
        //逐个删除gen_table_column中表id对应的记录
        //根据gen_table表中的表名获取表id删除gen_table表中记录：多个
        genTableList.stream().forEach(genTable -> {
            String tableId = genTable.getTableId();
            genTableColumnService.deleteGenTableColumnByGenTableId(tableId);
            genTableService.deleteGenTableByGenTableId(tableId);
        });
        //根据databaseId删除t_database表中对应记录
        dataBaseMapper.deleteDatabaseByDatabaseId(databaseId);
        //根据databaseId删除t_project表中对应记录
        dataBaseMapper.deleteProjectByDatabaseId(databaseId);
        //重新调用导入数据库功能
        database.setId(idWorker.nextId() + "");
        add(database);
        return Response.success("新表加载成功");
    }
}
